
package picasa;
/*import java.awt.*;
 import java.awt.color.ColorSpace;
 import java.awt.event.*;
 import java.awt.geom.AffineTransform;
 import java.awt.image.*;
 import java.awt.Toolkit;
 import java.net.*;
 import java.nio.Buffer;
 import javax.servlet.*;
 import javax.swing.*;
 */


import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageFilter;
import java.awt.image.FilteredImageSource;
import java.io.File;
import java.io.PrintWriter;
import java.util.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



public class EditServlet extends HttpServlet {

	
	EditFunctions ef = new EditFunctions();
	databaseConnection db = new databaseConnection();
	
	static Integer counter;
	public void doGet (HttpServletRequest req, HttpServletResponse res){
		
		doPost (req, res);
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response){	
		//System.out.println("inside editservlet");

		
		HttpSession session = request.getSession(true);
		String fileName = "C://workspace//Picasa//ImageLibraries//Farzana//IMG_1882.JPG";
		File file = new File(fileName);
		BufferedImage image; 
		String function = request.getParameter("function");
		
		
		try{
		PrintWriter out = response.getWriter();	
		image = ImageIO.read(file);
		//first visit of page
		if(session.getAttribute("isOriginal") == (null)){
			
			
			
			session.setAttribute("isOriginal", true);
		}
		
		if(session.getAttribute("isOriginal").equals(true)){
			ef.setIsOriginal(true);
			
			
		}else{
			ef.setIsOriginal(false);
		}
		
		
			//save the image and get rid of all temporary images
			//once this action is take u cannot go back to original or unde
			if (function.equals("save")){
				ef.finalSave();
				session.setAttribute("isOriginal", true);
			}else if (function.equals("undo")){
				ef.undo();
			}else if (function.equals("rotate")){
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
				
				
				//ef.rotateImage(image,Double.parseDouble(request.getParameter("degrees")));
				image = Conversion.convert(image, BufferedImage.TYPE_INT_RGB);
				ef.rotateImage(image,90.0);
				session.setAttribute("isOriginal", false);
				
			}
			
			else if (function.equals("blueTint")){
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
				
				ef.colorTint(image,"blue");
				session.setAttribute("isOriginal", false);
			}else if (function.equals("greenTint")){
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
				
				ef.colorTint(image,"green");
				session.setAttribute("isOriginal", false);
			}else if (function.equals("redTint")){
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
				
				ef.colorTint(image, "red");
				session.setAttribute("isOriginal", false);
			}
			else if(function.equals("bright_contrast")){
	
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
	
				ef.renameImage(image,1);
				ef.historyArray.add("bright_contrast");
				
				//get the contrast and brightness values from the webpage
				double contrast = Double.parseDouble(request.getParameter("contrast"));
				double brightness = Double.parseDouble(request.getParameter("brightness"));
				
				
				image = Conversion.convert(image,BufferedImage.TYPE_INT_ARGB);
				
				//the constructor requires a BufferedImage, Contrast Value, Brightness Value
				TuningEffects bc = new TuningEffects("ImageInterface",contrast, brightness/80);
				bc.Tuning(image);
                image = bc.returnImage();
                image = Conversion.convert(image,BufferedImage.TYPE_INT_RGB);
                ef.saveImage(image);
                
                session.setAttribute("isOriginal", false);
			}
			else if (function.equals("originalImage")){
				
				ef.backToOrigImage(image);
				session.setAttribute("isOriginal", true);
			
			}else if (function.equals("sepia")){
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
				ef.sepia(image);
				session.setAttribute("isOriginal", false);
			}
			else if (function.equals("bw")){
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
				ef.grayScale(image);
				session.setAttribute("isOriginal", false);
			
				
			}else if (function.equals("sharpen")){
				
				ef.sharpenImage(image);
				session.setAttribute("isOriginal", false);
				
			}else if (function.equals("emboss")){
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
				ef.EmbossImage(image);
				session.setAttribute("isOriginal", false);
				
			}else if (function.equals("crop")){
				
				String strX = request.getParameter("x");
				int x =Integer.parseInt( strX.substring(0,strX.indexOf("px")));
				
				String strY = request.getParameter("y");
				int y =Integer.parseInt( strY.substring(0,strY.indexOf("px")));
				
				String strH = request.getParameter("h");
				int h =Integer.parseInt( strH.substring(0,strH.indexOf("px")));
				
				String strW = request.getParameter("w");
				int w =Integer.parseInt( strW.substring(0,strW.indexOf("px")));
				
				System.out.println(x+" "+y+" " + w+ " " + h);
				
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
	
				ef.crop(image,x,y,w,h);
				session.setAttribute("isOriginal", false);
				
			}else if (function.equals("invert")){
				
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
				
				
				
				ef.invertImage(image);
				session.setAttribute("isOriginal", false);
				
			}else if (function.equals("color")){
				
				if(session.getAttribute("isOriginal").equals(true)){
					ef.saveImage(image);
				}
	
				ef.renameImage(image,1);
				ef.historyArray.add("color");
				
				//get the contrast and brightness values from the webpage
				int blue = Integer.parseInt(request.getParameter("blue"));
				int green = Integer.parseInt(request.getParameter("green"));
				int red = Integer.parseInt(request.getParameter("red"));
				
				image = Conversion.convert(image,BufferedImage.TYPE_INT_ARGB);
				
				
				TuningEffects bc = new TuningEffects("ColorInterface",red , blue, green);
				bc.Tuning(image);
                image = bc.returnImage();
                image = Conversion.convert(image,BufferedImage.TYPE_INT_RGB);
                ef.saveImage(image);
                
                session.setAttribute("isOriginal", false);
			}else if ( function.equals("login")){
				
				String user = request.getParameter("userid");
				String pass = request.getParameter("password");
				
				boolean valid = db.checkUser(user,pass);
				if (valid == true){
					db.login(user);
				}
				response.setContentType("text/xml");
				response.setHeader("Cache-Control","no-cache");
				
				out.println("<response>");
				out.println("<valid>" + Boolean.toString(valid) + "</valid>");
				out.println("</response>");
				out.close(); 
			}else if (function.equals("logout")){
				//Resets the user session information
				String user = request.getParameter("userid");
				db.logout(user);
			}else if (function.equals("createUser")){
			
				String user = request.getParameter("userid");
				String fname = request.getParameter("fname");
				String lname = request.getParameter("lname");
				String strAdd = request.getParameter("strAdd");
				String city = request.getParameter("city");
				String zipcode = request.getParameter("zipcode");
				String emailAdd = request.getParameter("emailAdd");
				String pass = request.getParameter("pass");
				
				boolean newName = db.checkId(user);
				if (newName == true){
					db.addUser(user, pass, fname,lname, strAdd, city, zipcode, emailAdd);
					response.setContentType("text/xml");
					response.setHeader("Cache-Control","no-cache");
					
					out.println("<response>");
					out.println("<valid>" + Boolean.toString(newName) + "</valid>");
					out.println("</response>");
					out.close(); 
				}else{
					System.out.println("Inside else statement");
					response.setContentType("text/xml");
					response.setHeader("Cache-Control","no-cache");
					
					out.println("<response>");
					out.println("<valid>" + Boolean.toString(newName) + "</valid>");
					out.println("<message> The User Name that you have chosen is already in use. Please change User Name.</message>");
					out.println("</response>");
					out.close(); 
				}
				
			}else if (function.equals("setFolders")){
					List folders = new ArrayList();
					List numbers = new ArrayList();
					String user = request.getParameter("userid");
					boolean valid = db.checkId(user);
					StringBuffer results = new StringBuffer("<folders>");
					if(Boolean.toString(valid) == "false"){
						folders = db.getFolders(user);
						numbers = db.getNumbers(user);
						for(int i = 0; i < folders.size(); i++){
							results.append("<folder>");
							results.append(folders.get(i));
							results.append("</folder>");
							results.append("<number>");
							results.append(numbers.get(i));
							results.append("</number>");
						}
						results.append("</folders>");
						
						response.setContentType("text/xml");
						out.write(results.toString());
						out.close();
					}
			}else if(function.equals("getPhotos")){
				StringBuffer results;
				String user = request.getParameter("userid");
				results = db.getPhotos(user);
				response.setContentType("text/xml");
				out.write(results.toString());
				out.close();
			}
		
		}catch(Exception e){ 
			System.out.println ("inside doPost error");
			System.out.println(e.toString());
			
		}
		response.setContentType("text/html");
		try{
			ServletOutputStream out1 = response.getOutputStream();
		
		}catch (Exception e){
			
		}
		
		
	}
	
}

